﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
using System.Runtime.Serialization;
using WidgetInterface;

/// <summary>
/// Author: AustralTek 
/// Create date: 2010/05/04
/// http://www.australtek.com
/// Copyright: 2009 - 2010
/// Summary description for Chart
/// </summary>

namespace IndustrialDashboard
{
    [Serializable]
    public class Chart : IWidget
    {
        private String strSource = "ChartSWF  ";
        public List<String> fields { get; set; }
        public List<Dictionary<String, Object>> data { get; set; }        
        public Chart()
        {
            //
            // TODO: Add constructor logic here
            //
            Logger.Info(strSource, "Initializing");
            fields = new List<string> { };
            data = new List<Dictionary<String, Object>> { };            
        }
        public Chart(DataSet ds)
        {
            //
            // TODO: Add constructor logic here
            //
            Logger.Info(strSource, "Initializing");
            fields = new List<string> { };
            data = new List<Dictionary<String, Object>> { };            
            this.FillChart(ds);
        }
        private void FillChart(DataSet ds)
        {
            try
            {
                if (ds != null)
                {
                    if (ds.Tables.Count > 0)
                    {
                        if ((ds.Tables[0].Columns.Count > 0) && (ds.Tables[0].Rows.Count > 0))
                        {
                            foreach (DataColumn dc in ds.Tables[0].Columns) fields.Add(dc.ColumnName);
                            for (var i = 0; i < ds.Tables[0].Rows.Count; i++)
                            {
                                Dictionary<String, Object> dic = new Dictionary<String, Object> { };
                                for (int c = 0; c < ds.Tables[0].Columns.Count; c++) dic.Add(ds.Tables[0].Columns[c].ColumnName, ds.Tables[0].Rows[i].ItemArray[c]);
                                data.Add(dic);
                            }
                        }
                    }
                }
                else
                {
                    Logger.Warn(strSource, "The DataSet is null");
                }
            }
            catch (Exception ex)
            {
                Logger.Warn(strSource, "",ex);
                Logger.Error(strSource,"", ex);
            }
        }
        private string[,] toStringMatrix(DataTable dt)
        {
            try
            {
                string myPattern = "&#";
                string myReplace = "&amp#";
                Regex myRegExp = new Regex(myPattern);
                string[,] mat = new string[dt.Rows.Count + 1, dt.Columns.Count];
                int i = 0;
                int j = 0;
                foreach (DataColumn dc in dt.Columns)
                {
                    mat[i, j] = dc.ColumnName.Trim();
                    j++;
                }
                i++;
                foreach (DataRow r in dt.Rows)
                {
                    j = 0;
                    foreach (object item in r.ItemArray)
                    {
                        mat[i, j] = myRegExp.Replace(item.ToString().Trim(), myReplace);
                        j++;
                    }
                    i++;
                }
                return mat;
            }
            catch (FormatException fe)
            {
                Logger.Warn(strSource, "The Chart Matrix has not the correct format", fe);
                Logger.Error(strSource, "The Chart Matrix has not the correct format", fe);
                throw new UserException("The Chart Matrix has not the correct format. Error: " + fe.Message);                
            }
            catch (Exception ex)
            {
                Logger.Warn(strSource,"The Chart Matrix has not the correct format", ex);
                Logger.Error(strSource,"The Chart Matrix has not the correct format", ex);
                throw new Exception("The Chart Matrix has not the correct format. Error: " + ex.Message);                
            }
        }
        #region "Implements"
        public object GenerateResponse(System.Data.DataSet ds)
        {
            Chart ch = new Chart(ds);
            return (Object)ch;
        }
        public object GetExcelChunk(System.IO.Stream document)
        {
            Logger.Warn(strSource, "GetExcelChunk() [NotImplementedException]");
            throw new NotImplementedException();     
        }
        public object GetPDFChunk(System.IO.Stream document)
        {
            Logger.Warn(strSource, "GetPDFChunk() [NotImplementedException]");
            throw new NotImplementedException();                       
        }
        #endregion
    }
}